/*
 *  $Id$

    Copyright (C) 2008  Nathaniel Waisbrot

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package termint.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

import termint.gui.window.Window;

/**
 * @author Nathaniel Waisbrot
 *
 */
public class MainPalate extends Shell {
	/**
	 * Create the shell.
	 * @param display
	 */
	public MainPalate(Display display) {
		super(display, SWT.SHELL_TRIM); //|SWT.TOOL|SWT.TITLE);
		setLayout(new FillLayout(SWT.VERTICAL));
		
		Menu menu = new Menu(this, SWT.BAR);
		setMenuBar(menu);
		
		MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
		mntmFile.setText("File");
		
		Menu menu_1 = new Menu(this, SWT.DROP_DOWN);
		mntmFile.setMenu(menu_1);
		
		MenuItem mntmQuit = new MenuItem(menu_1, SWT.NONE);
		mntmQuit.setText("Quit");
		
		MenuItem junk = new MenuItem(menu_1, SWT.NONE);
		junk.setText("Some junk");
		
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("Interfaces");
		setSize(147, 300);
	}
	
	public void addWindow(final Window shell, String title) {
		final Button button = new Button(this, SWT.TOGGLE);
		button.setText(title);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				if (button.getSelection()) {
					shell.setVisible(true);
					shell.setActive();
				} else {
					shell.setVisible(false);
				}
			}
		});
		shell.addShellListener(new ShellAdapter(){
			public void shellActivated(ShellEvent arg0) {
				if (!button.isDisposed())
					button.setSelection(true);
			}
			public void shellClosed(ShellEvent arg0) {
				button.setSelection(false);
			}
		});
		shell.open();
		button.setSelection(true);
		this.pack();
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
